/*
  Copyright (c) 2009 Dave Gamble
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
 
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/

#ifndef OSC_cJSON__h
#define OSC_cJSON__h

#ifdef __cplusplus
extern "C"
{
#endif

/* OSC_cJSON Types: */
#define OSC_cJSON_False 0
#define OSC_cJSON_True 1
#define OSC_cJSON_NULL 2
#define OSC_cJSON_Number 3
#define OSC_cJSON_String 4
#define OSC_cJSON_Array 5
#define OSC_cJSON_Object 6
	
#define OSC_cJSON_IsReference 256
#define OSC_OSC_cJSON_StringIsConst 512

/* The OSC_cJSON structure: */
typedef struct OSC_cJSON {
    struct OSC_cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct OSC_cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

	int type;					/* The type of the item, as above. */

    char *valuestring;			/* The item's string, if type==OSC_cJSON_String */
    int valueint;				/* The item's number, if type==OSC_cJSON_Number */
    double valuedouble;			/* The item's number, if type==OSC_cJSON_Number */

	char *string;				/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} OSC_cJSON;

typedef struct OSC_cJSON_Hooks {
      void *(*malloc_fn)(size_t sz);
      void (*free_fn)(void *ptr);
} OSC_cJSON_Hooks;

/* Supply malloc, realloc and free functions to OSC_cJSON */
extern void OSC_cJSON_InitHooks(OSC_cJSON_Hooks* hooks);
/* Supply a block of JSON, and this returns a OSC_cJSON object you can interrogate. Call OSC_cJSON_Delete when finished. */
extern OSC_cJSON *OSC_cJSON_Parse(const char *value);
/* Render a OSC_cJSON entity to text for transfer/storage. Free the char* when finished. */
extern char  *OSC_cJSON_Print(OSC_cJSON *item);
/* Render a OSC_cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
extern char  *OSC_cJSON_PrintUnformatted(OSC_cJSON *item);
/* Render a OSC_cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
extern char *OSC_cJSON_PrintBuffered(OSC_cJSON *item,int prebuffer,int fmt);
/* Delete a OSC_cJSON entity and all subentities. */
extern void   OSC_cJSON_Delete(OSC_cJSON *c);

/* Returns the number of items in an array (or object). */
extern int	  OSC_cJSON_GetArraySize(OSC_cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
extern OSC_cJSON *OSC_cJSON_GetArrayItem(OSC_cJSON *array,int item);
/* Get item "string" from object. Case insensitive. */
extern OSC_cJSON *OSC_cJSON_GetObjectItem(OSC_cJSON *object,const char *string);

/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when OSC_cJSON_Parse() returns 0. 0 when OSC_cJSON_Parse() succeeds. */
extern const char *OSC_cJSON_GetErrorPtr(void);
	
/* These calls create a OSC_cJSON item of the appropriate type. */
extern OSC_cJSON *OSC_cJSON_CreateNull(void);
extern OSC_cJSON *OSC_cJSON_CreateTrue(void);
extern OSC_cJSON *OSC_cJSON_CreateFalse(void);
extern OSC_cJSON *OSC_cJSON_CreateBool(int b);
extern OSC_cJSON *OSC_cJSON_CreateNumber(double num);
extern OSC_cJSON *OSC_cJSON_CreateString(const char *string);
extern OSC_cJSON *OSC_cJSON_CreateArray(void);
extern OSC_cJSON *OSC_cJSON_CreateObject(void);

/* These utilities create an Array of count items. */
extern OSC_cJSON *OSC_cJSON_CreateIntArray(const int *numbers,int count);
extern OSC_cJSON *OSC_cJSON_CreateFloatArray(const float *numbers,int count);
extern OSC_cJSON *OSC_cJSON_CreateDoubleArray(const double *numbers,int count);
extern OSC_cJSON *OSC_cJSON_CreateStringArray(const char **strings,int count);

/* Append item to the specified array/object. */
extern void OSC_cJSON_AddItemToArray(OSC_cJSON *array, OSC_cJSON *item);
extern void	OSC_cJSON_AddItemToObject(OSC_cJSON *object,const char *string,OSC_cJSON *item);
extern void	OSC_cJSON_AddItemToObjectCS(OSC_cJSON *object,const char *string,OSC_cJSON *item);	/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the OSC_cJSON object */
/* Append reference to item to the specified array/object. Use this when you want to add an existing OSC_cJSON to a new OSC_cJSON, but don't want to corrupt your existing OSC_cJSON. */
extern void OSC_cJSON_AddItemReferenceToArray(OSC_cJSON *array, OSC_cJSON *item);
extern void	OSC_cJSON_AddItemReferenceToObject(OSC_cJSON *object,const char *string,OSC_cJSON *item);

/* Remove/Detatch items from Arrays/Objects. */
extern OSC_cJSON *OSC_cJSON_DetachItemFromArray(OSC_cJSON *array,int which);
extern void   OSC_cJSON_DeleteItemFromArray(OSC_cJSON *array,int which);
extern OSC_cJSON *OSC_cJSON_DetachItemFromObject(OSC_cJSON *object,const char *string);
extern void   OSC_cJSON_DeleteItemFromObject(OSC_cJSON *object,const char *string);
	
/* Update array items. */
extern void OSC_cJSON_InsertItemInArray(OSC_cJSON *array,int which,OSC_cJSON *newitem);	/* Shifts pre-existing items to the right. */
extern void OSC_cJSON_ReplaceItemInArray(OSC_cJSON *array,int which,OSC_cJSON *newitem);
extern void OSC_cJSON_ReplaceItemInObject(OSC_cJSON *object,const char *string,OSC_cJSON *newitem);

/* Duplicate a OSC_cJSON item */
extern OSC_cJSON *OSC_cJSON_Duplicate(OSC_cJSON *item,int recurse);
/* Duplicate will create a new, identical OSC_cJSON item to the one you pass, in new memory that will
need to be released. With recurse!=0, it will duplicate any children connected to the item.
The item->next and ->prev pointers are always zero on return from Duplicate. */

/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
extern OSC_cJSON *OSC_OSC_cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);

extern void OSC_cJSON_Minify(char *json);

/* Macros for creating things quickly. */
#define OSC_cJSON_AddNullToObject(object,name)		OSC_cJSON_AddItemToObject(object, name, OSC_cJSON_CreateNull())
#define OSC_cJSON_AddTrueToObject(object,name)		OSC_cJSON_AddItemToObject(object, name, OSC_cJSON_CreateTrue())
#define OSC_cJSON_AddFalseToObject(object,name)		OSC_cJSON_AddItemToObject(object, name, OSC_cJSON_CreateFalse())
#define OSC_cJSON_AddBoolToObject(object,name,b)	OSC_cJSON_AddItemToObject(object, name, OSC_cJSON_CreateBool(b))
#define OSC_cJSON_AddNumberToObject(object,name,n)	OSC_cJSON_AddItemToObject(object, name, OSC_cJSON_CreateNumber(n))
#define OSC_cJSON_AddStringToObject(object,name,s)	OSC_cJSON_AddItemToObject(object, name, OSC_cJSON_CreateString(s))

/* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define OSC_cJSON_SetIntValue(object,val)			((object)?(object)->valueint=(object)->valuedouble=(val):(val))
#define OSC_cJSON_SetNumberValue(object,val)		((object)?(object)->valueint=(object)->valuedouble=(val):(val))

#ifdef __cplusplus
}
#endif

#endif
